import 'package:flutter/material.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/routes/conversation/view/xindong_liaotian_message_score_container.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/utils/xindong_text_util.dart';
import 'package:xindong/widgets/image/browser/xindong_images_browser.dart';
import 'package:xindong/widgets/video/xindong_video_player_dialog.dart';
import 'package:tencent_cloud_chat_uikit/tencent_cloud_chat_uikit.dart';
import 'package:transparent_image/transparent_image.dart';

// 自定义图片、视频项
class XinDongLiaoTianMessageImageVideo extends StatelessWidget {
  final String url;
  final String? video;
  final V2TimMessage message;

  const XinDongLiaoTianMessageImageVideo({Key? key, required this.url, this.video, required this.message})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    final double aspectRatio =
        ImageUtil.imageSizeAspectRatioFromUrl(imageUrl: url, start: "size:", end: ":size", limitScale: 9 / 15);

    final isFromSelf = message.isSelf == true;
    return XinDongLiaoTianMessageScoreContainer(
      hidden: isFromSelf,
      message: message,
      child: GestureDetector(
        onTap: () async {
          if (TextUtil.isEmpty(video)) {
            XinDongImagesBrowser.show(list: [url], currentIndex: 0);
          } else {
            XinDongVideoPlayerDialog.show(videoUrl: video!, coverUrl: url);
          }
        },
        child: SizedBox(
          width: aspectRatio > 1.0 ? 198 : 100,
          child: AspectRatio(
            aspectRatio: aspectRatio,
            child: Container(
              clipBehavior: Clip.antiAlias,
              decoration: const BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(8)),
              ),
              child: Stack(
                alignment: Alignment.center,
                fit: StackFit.expand,
                children: [
                  ImageUtil.networkImageUrl(url,
                      errorWidget: errorDisplay(context),
                      fit: BoxFit.fitWidth,
                      placeholder: Image(image: MemoryImage(kTransparentImage))),
                  // CachedNetworkImage(
                  //   imageUrl: this.url,
                  //   // use small image in message list as priority
                  //   errorWidget: (context, error, stackTrace) => errorDisplay(context),
                  //   fit: BoxFit.fitWidth,
                  //   placeholder: (context, url) => Image(image: MemoryImage(kTransparentImage)),
                  //   fadeInDuration: const Duration(milliseconds: 0),
                  // ),
                  TextUtil.isEmpty(video)
                      ? const SizedBox()
                      : Container(
                          width: double.infinity,
                          height: double.infinity,
                          alignment: Alignment.center,
                          decoration: const BoxDecoration(color: Colors.black38),
                          child: ImageUtil.asset(AssetsRes.ICON_VIDEO_PLAY, width: 40, height: 40),
                        ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget errorDisplay(BuildContext context) {
    return Container(
      decoration: BoxDecoration(border: Border.all(width: 1, color: Colors.black12)),
      height: 100,
      child: const Center(
        child: Text(
          "图片加载失败",
          style: TextStyle(color: DZColors.mainColor),
        ),
      ),
    );
  }
}
